// Constructs an income panel for couples with DV report 
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear


// Analysis 
g av_prior_emp = (ptoim1B5 + ptoim1B4 + ptoim1B3 + ptoim1B2 + ptoim1B1)/5
g av_prior_inc = (tyotuB5 + tyotuB3 + tyotuB3 + tyotuB2 + tyotuB1)/5

g av_prior_emp1 = ptoim1B1
g av_prior_inc1 = tyotuB1

sum age, d
local med=r(p50)
g age_above_med=(age>=`med')
g age_group=1 if age<30
replace age_group=2 if age<40 & age>=30 
replace age_group=3 if age>=40

sum spouse_pre_age, d
local med=r(p50)


xtile sp_earn_group = spouse_pre_tyotu, nq(5)

g sp_age_above_med=(spouse_pre_age>=`med')
g sp_age_group=1 if spouse_pre_age<30
replace sp_age_group=2 if spouse_pre_age<40 & spouse_pre_age>=30 
replace sp_age_group=3 if spouse_pre_age>=40

g gap_emp="EE" if spouse_pre_ptoim1==1 & ptoim1B1==1
replace gap_emp="EU" if spouse_pre_ptoim1==1 & ptoim1B1==0
replace gap_emp="UE" if spouse_pre_ptoim1==0 & ptoim1B1==1
replace gap_emp="UU" if spouse_pre_ptoim1==0 & ptoim1B1==0

g gap_edu="CC" if spouse_college==1 & college==1
replace gap_edu="CN" if spouse_college==1 & college==0
replace gap_edu="NC" if spouse_college==0 & college==1
replace gap_edu="NN" if spouse_college==0 & college==0


g gap_age=spouse_pre_age-(age-1)

sum gap_age, d
local med=r(p50)
g gap_age_above_med=(gap_age>=`med')

g nofert_change2 = (nchildB1 == nchildF2)
g nofert_change5 = (nchildB1 == nchildF5)
g novickid = (nchildB1==0)


local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummy used in the eventstudies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

gen treatPost= treat==1 & time_>=0
lab var treatPost "Abusive x Cohabit"
egen full_fe = group(match_id time)

global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_1 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"


// Earnings
xtile earn_group = av_prior_inc, nq(5)

forvalues i=1/5 {
	
	reghdfe ptoim1 treatPost treat if earn_group ==`i',  absorb($fe)  cluster($cluster)
	est sto ptoim1_`i'

	
}

set scheme s2mono

coefplot (ptoim1_1, label("Q1") ) ///
				 (ptoim1_2, label("Q2")) ///
				 (ptoim1_3, label("Q3") ) ///
				 (ptoim1_4, label("Q4")) ///
				  (ptoim1_5, label("Q5") ), ///
			recast(bar) citop ciopts(recast(rcap) color(black)) barwidth(0.12) ///
			keep(treatPost) name(ptoim1_earn, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.12(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_earn5group_ptoim1.pdf", replace

// Education 

foreach thing in college high_school ho {
	
	reghdfe ptoim1 treatPost treat if `thing' ==1,  absorb($fe)  cluster($cluster)
	est sto ptoim1_`thing'
}


coefplot (ptoim1_ho, label("Drop Out") bcolor(blue)) ///
				 (ptoim1_high_school, label("High School") bcolor(red)) ///
				  (ptoim1_college, label("College") bcolor(green)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_educ_ptoim1.pdf", replace


// Perp Earnings
drop earn_group
xtile earn_group = spouse_pre_tyotu, nq(5)
forvalues i=1/5 {
	
	reghdfe ptoim1 treatPost treat if earn_group ==`i',  absorb($fe)  cluster($cluster)
	est sto ptoim1_`i'

	
}

coefplot (ptoim1_1, label("Q1") bcolor(blue)) ///
				 (ptoim1_2, label("Q2") bcolor(red)) ///
				 (ptoim1_3, label("Q3") bcolor(green)) ///
				 (ptoim1_4, label("Q4") bcolor(brown)) ///
				  (ptoim1_5, label("Q5") bcolor(purple)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.12) ///
			keep(treatPost) name(ptoim1_earn, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.12(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_perpearn5group_ptoim1.pdf", replace


// Perp Education 

foreach thing in college high_school ho {
	
	reghdfe ptoim1 treatPost treat if spouse_`thing' ==1,  absorb($fe)  cluster($cluster)
	est sto ptoim1_`thing'
}


coefplot (ptoim1_ho, label("Drop Out") bcolor(blue)) ///
				 (ptoim1_high_school, label("High School") bcolor(red)) ///
				  (ptoim1_college, label("College") bcolor(green)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_perpeduc_ptoim1.pdf", replace

// Age

foreach thing in ptoim1 tyotu{
	reghdfe `thing' treatPost treat if sp_age_group ==3 ,  absorb($fe)  cluster($cluster)
	est sto `thing'_old
	
	reghdfe `thing' treatPost treat if sp_age_group ==2,  absorb($fe)  cluster($cluster)
	est sto `thing'_middle
	
    reghdfe `thing' treatPost treat if sp_age_group ==1,  absorb($fe)  cluster($cluster)
	est sto `thing'_young
	
}


coefplot (ptoim1_young, label("Age<30") bcolor(green)) ///
				 (ptoim1_middle, label("Age>=30 & <40") bcolor(red)) ///
				  (ptoim1_old, label("Age>=40") bcolor(blue)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_perpage_ptoim1.pdf", replace

// Crimes

g match_prec_ = 0
replace match_prec_ = spouse_pre_crimes if dv_couple==1
bys match_id : egen match_prec = max(match_prec_)
	reghdfe ptoim1 treatPost treat if match_prec ==0 ,  absorb($fe)  cluster($cluster)
	est sto ptoim1_0
	reghdfe ptoim1 treatPost treat if match_prec >0 ,  absorb($fe)  cluster($cluster)
	est sto ptoim1_1
	

coefplot (ptoim1_0, label("Spouse - No Prior") bcolor(red)) ///
				 (ptoim1_1, label("Spouse - Prior") bcolor(blue)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.12 0)) ylabel(-0.12(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_perpcrime_ptoim1.pdf", replace

drop match_prec*
g match_prec_ = 0
replace match_prec_ = pre_crimes if dv_couple==1
bys match_id : egen match_prec = max(match_prec_)
	reghdfe ptoim1 treatPost treat if match_prec ==0 ,  absorb($fe)  cluster($cluster)
	est sto ptoim1_0
	reghdfe ptoim1 treatPost treat if match_prec >0 ,  absorb($fe)  cluster($cluster)
	est sto ptoim1_1
	

coefplot (ptoim1_0, label("Victim - No Prior") bcolor(red)) ///
				 (ptoim1_1, label("Victim - Prior") bcolor(blue)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.12 0)) ylabel(-0.12(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\victim_match_het_victimcrime_ptoim1.pdf", replace



reghdfe ptoim1 treatPost treat,  absorb($fe)  cluster($cluster)
est sto main1
su ptoim1 if treat==0 & time_==-1
local pmean = round(r(mean), 0.001)
estadd local premean "`pmean'"

reghdfe ptoim1 treatPost treat if match_prec==0,  absorb($fe)  cluster($cluster)
est sto robust_0
su ptoim1 if treat==0 & time_==-1 & match_prec==0
local pmean = round(r(mean), 0.001)
estadd local premean "`pmean'"

g match_prec_s = 0
replace match_prec_s = spouse_pre_crimes if dv_couple==1
bys match_id : egen match_prec_spouse = max(match_prec_s)

reghdfe ptoim1 treatPost treat if match_prec_spouse==0,  absorb($fe)  cluster($cluster)
est sto robust_1
su ptoim1 if treat==0 & time_==-1 & match_prec_spouse==0
local pmean = round(r(mean), 0.001)
estadd local premean "`pmean'"

reghdfe ptoim1 treatPost treat if match_prec_spouse==0 & match_prec==0,  absorb($fe)  cluster($cluster)
est sto robust_2
su ptoim1 if treat==0 & time_==-1 & match_prec_spouse==0 & match_prec==0
local pmean = round(r(mean), 0.001)
estadd local premean "`pmean'"


esttab main1 robust_0 robust_1 robust_2 using "$result_folder\victim_priorcrime_robust.tex", replace b(4) se(4) r2(4) ///
			keep(treatPost treat) scalars("premean Dependent Mean")
			
			
// children 
g change2 = 0
replace change2 = nofert_change2 if dv_couple==1
bys match_id : egen match_change2 = max(change2)

reghdfe ptoim1 treatPost treat if match_change2==1,  absorb($fe)  cluster($cluster)
su ptoim1 if treat==0 & time_==-1 & match_change2==1

g change5 = 0
replace change5 = nofert_change5 if dv_couple==1
bys match_id : egen match_change5 = max(change5)

reghdfe ptoim1 treatPost treat if match_change5==1,  absorb($fe)  cluster($cluster)
su ptoim1 if treat==0 & time_==-1 & match_change5==1

reghdfe ptoim1 treatPost treat if novickid==1,  absorb($fe)  cluster($cluster)
su ptoim1 if treat==0 & time_==-1 & novickid==1
